package com.wefans.wefansapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.wefans.wefansapp.R;

public class TutorialActivity extends BaseActivity {

	final int img_draw[] ={
			R.drawable.tutorial_01,
			R.drawable.tutorial_02,
			R.drawable.tutorial_03,
			R.drawable.tutorial_04,
			R.drawable.tutorial_05,
			};
	
	private ViewPager mPager = null;
	private CustomPagerAdapter mPagerAdt = null;
	
	private ImageView mIvStart = null;
	private ImageButton mBtnNext = null;
	private ImageButton mBtnSkip = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_tutorial);
		
		mPager = (ViewPager) findViewById(R.id.VP_PAGER);
		mIvStart = (ImageView) findViewById(R.id.BTN_START);
		mBtnSkip = (ImageButton) findViewById(R.id.BTN_SKIP);
		mBtnNext = (ImageButton) findViewById(R.id.BTN_NEXT);
		
		mIvStart.setVisibility(View.GONE);
		mBtnSkip.setVisibility(View.VISIBLE);
		mBtnNext.setVisibility(View.VISIBLE);
		
		mIvStart.setOnClickListener(mClickListener);
		mBtnSkip.setOnClickListener(mClickListener);
		mBtnNext.setOnClickListener(mClickListener);
		
		mPager.setAdapter(new CustomPagerAdapter(this));
		mPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			public void onPageSelected(int arg0) {
				boolean bLast = arg0 >= img_draw.length - 1;
					mIvStart.setVisibility(bLast ? View.VISIBLE : View.GONE);
					mBtnSkip.setVisibility(bLast ? View.GONE : View.VISIBLE);
					mBtnNext.setVisibility(bLast ? View.GONE : View.VISIBLE);
			}
			
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	OnClickListener mClickListener = new OnClickListener() {
		
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.BTN_START:
				Intent it = new Intent(TutorialActivity.this, LoginActivity.class);
				startActivity(it);
				finish();
				break;
			case R.id.BTN_SKIP:
				it = new Intent(TutorialActivity.this, LoginActivity.class);
				startActivity(it);
				finish();
				break;
			case R.id.BTN_NEXT:
				if (mPager != null) {
					mPager.setCurrentItem(mPager.getCurrentItem() + 1, true);
				}
				break;
			}
		}
	};
	
	private class CustomPagerAdapter extends PagerAdapter {

		private Context mContext = null;
		
		public CustomPagerAdapter(Context context) {
			mContext = context;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return img_draw.length;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {

			return arg0 == arg1; 
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			
			ImageView iv= null;
			
			iv = new ImageView(mContext);
			iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			iv.setBackgroundResource(img_draw[position]);
			
			((ViewPager)container).addView(iv, 0);
			
			return iv;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {

			((ViewPager)container).removeView((View)object);
		}
		
	}
}
